Socket
Socket
Sign inDemoInstall

meta-shortcodes

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

meta-shortcodes

Shortcode parser with support of arguments, key-value attributes and nesting.


Version published
Weekly downloads
205
increased by40.41%
Maintainers
1
Weekly downloads
 
Created
Source

#meta-shortcodes

Generic shortcodes parser with support of attributes and single/pair tags.

Usage

var should = require("should");
var ShortcodeParser = require("meta-shortcodes");

var parser = ShortcodeParser();

parser.add("test", function(opts, content){
	return content.toUpperCase();
});

parser.add("nested", function(opts, content){
	
	if(!opts.multiply) return "Missing multiply attribute!";

	var out = [];

	for(var i = 0; i < opts.length; i++)
		out.push(opts[i] * parseFloat(opts.multiply));

	return out.join(" ");

});

var input = "Sample [test]shortcode content [nested multiply=2 2 4/] is upper[/test] case!";
var output = parser.parse(input);

output.should.eql("Sample SHORTCODE CONTENT 4 8 IS UPPER case!");

Instalation

npm install meta-shortcodes

API

#constructor(opts)

opts = {
	openPattern: '\\[',
	closePattern: '\\]'
}

#add(shortcodeName, handlerFunction)

Registers new shortcode

ParamTypeDescription
shortcodeNamestringName of shortcode tag
handlerFunctionfunctionFunction that returns replacement for shortcode. Accepts two arguments - options object and content string.

#parse(inputStr)

Registers new shortcode

ParamTypeDescription
inputStrstringInput string where shortcodes should be replaced.

Shortcode examples

#Self-closing
[name argument1 argument2 key=value key=value/]

#Pair
[name]content[/name]

#Quoted arguments
[name "argument 1" 'argument 2' "key is"="long value"/]

#Escaping quotes in arguments
[name "argu\"ment" "ke\"y"='val\'ue']

#Escaping of entire shortcode
\\[this shortcode is not processed /]

Testing

npm install --dev
npm test

Current code-coverage 97%

Using alternative brackets

var should = require("should");
var ShortcodeParser = require("meta-shortcodes");

var parser = ShortcodeParser({
	openPattern: '\\{{',
	closePattern: '\\}}'
});

parser.add("test", function(opts, content){
	return content.toUpperCase();
});

var output = parser.parse("Sample {{test}}upper{{/test}} case!").should.eql("Sample UPPER case!");

License

MIT (c) 2015 META Platform team, Jiri Hybek

Keywords

FAQs

Package last updated on 31 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc